package fileexplorer.controller;

import com.jfoenix.animation.alert.JFXAlertAnimation;
import com.jfoenix.controls.JFXAlert;
import com.jfoenix.controls.JFXDialogLayout;
import fileexplorer.FileExplorer;
import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Circle;
import javafx.stage.Modality;

import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.io.File;

public class FileExplorerController {

    private FileExplorer fileExplorer;
    private String currentPath;

    @FXML
    private Button btn_ok;
    @FXML
    private Button btn_new;
    @FXML
    private TableView table;
    @FXML
    private TreeView tree;
    @FXML
    private TextField input;
    @FXML
    private ChoiceBox select;
    @FXML
    private Circle btn_min;
    @FXML
    private Circle btn_max;
    @FXML
    private Circle btn_close;

    public void init(FileExplorer fileExplorer){
        setFileExplorer(fileExplorer);
        currentPath = "";
    }

    public TreeItem initFileNode(){
        TreeItem treeItem = new TreeItem<>("根目录");
        treeItem.setGraphic(addIcon(false));
        treeItem.setExpanded(true);
        addFileNode(treeItem);
        initLeftTree(treeItem);
        return treeItem;
    }

    @FXML
    private void hint(){

        JFXDialogLayout layout = new JFXDialogLayout();
        layout.setBody(new javafx.scene.control.Label("Hello World!"));
        final JFXAlert<Void> alert = new JFXAlert<>(fileExplorer.getStage());
        alert.setOverlayClose(true);
        alert.setAnimation(JFXAlertAnimation.CENTER_ANIMATION);
        alert.setContent(layout);
        alert.initModality(Modality.NONE);
        alert.showAndWait();
    }

    public FileExplorer getFileExplorer() {
        return fileExplorer;
    }

    public void setFileExplorer(FileExplorer fileExplorer) {
        this.fileExplorer = fileExplorer;
    }

    private void addFileNode(TreeItem treeItem){
        File[] files = new File[0];
        if (currentPath.equals("")){
            files = File.listRoots();
            FileSystemView fsv = FileSystemView.getFileSystemView();
            for (File f: files){
                TreeItem item = new TreeItem(fsv.getSystemDisplayName(f));
                item.setGraphic(addIcon("/icon/disk.png"));
                treeItem.getChildren().add(item);
                currentPath = f.getAbsolutePath();
                addFileNode(item);
            }
        }else {
            File file = new File(currentPath);
            if (file.exists()){
                files = file.listFiles();
            }
            for (File f: files){
                TreeItem item = new TreeItem(f.getName());
                if (f.isFile())item.setGraphic(addIcon(true));
                else item.setGraphic(addIcon(false));
                treeItem.getChildren().add(item);
            }
        }
    }

    private ImageView addIcon(boolean isFile){
        String path = "/icon/folder.png";
        if (isFile)path = "/icon/file.png";
        ImageView folderIcon = new ImageView();
        Image folderImage = new Image(getClass().getResourceAsStream(path));
        folderIcon.setImage(folderImage);
        folderIcon.setFitWidth(16);
        folderIcon.setFitHeight(16);
        return folderIcon;
    }

    private ImageView addIcon(String path){
        ImageView folderIcon = new ImageView();
        Image folderImage = new Image(getClass().getResourceAsStream(path));
        folderIcon.setImage(folderImage);
        folderIcon.setFitWidth(16);
        folderIcon.setFitHeight(16);
        return folderIcon;
    }

    @FXML
    public void closeWindow(){
        fileExplorer.close();
    }

    private void initLeftTree(TreeItem treeItem){
        TreeItem download = new TreeItem("下载");
        TreeItem desktop = new TreeItem("桌面");
        TreeItem document = new TreeItem("文档");
        File[] files = new File[0];
        currentPath = "C:"+File.separator+"Users"+File.separator+System.getProperty("user.name")+File.separator+"Downloads";
        download.setGraphic(addIcon("/icon/download.png"));
        addFileNode(download);
        currentPath = "C:"+File.separator+"Users"+File.separator+System.getProperty("user.name")+File.separator+"Desktop";
        desktop.setGraphic(addIcon("/icon/desktop.png"));
        addFileNode(desktop);
        currentPath = "C:"+File.separator+"Users"+File.separator+System.getProperty("user.name")+File.separator+"Documents";
        document.setGraphic(addIcon("/icon/document.png"));
        addFileNode(document);
        treeItem.getChildren().addAll(desktop,document,download);
    }
}
